<div class="layui-form seller-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">公告标题：</label>
            <div class="layui-input-inline seller-inline-4">
                <input type="text" name="title" lay-verify="title" placeholder="请输入标题关键字" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">更新时间：</label>
            <div class="layui-input-inline seller-inline-4">
                <input type="text" id="date" name="ctime" lay-verify="title" placeholder="开始时间 到 结束时间"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="notice-search"><i
                    class="iconfont icon-chaxun"></i>筛选</button>

            <button type="button" class="layui-btn layui-btn-sm add-notice"><i class="layui-icon">&#xe608;</i>
                添加</button>
        </div>
    </div>
</div>
<div class="table-body">
    <table id="noticeTable" lay-filter="noticeTable"></table>
</div>
<script>
    layui.use(['table', 'form', 'laydate'], function () {
        var table = layui.table, form = layui.form, date = layui.laydate;
        table.render({
            elem: '#noticeTable',
            height: 'full-99',
            cellMinWidth: '80',
            url: "{:url('Notice/index')}", //数据接口
            id: "noticeTable",
            page: true,//开启分页
            limit: '20',
            cols: [[ //表头
                { field: 'id', width: 90, title: 'ID', align: 'center' },
                { field: 'title', title: '公告标题', align: 'center' },
                { field: 'content', title: '公告描述内容', align: 'center' },
                { field: 'ctime', title: '创建时间', width: 170, sort: true, align: 'center' },
                { field: 'sort', title: '排序', width: 80, sort: true, align: 'center' },
                { field: 'right', title: '操作', width: 140, align: 'center', toolbar: '#notice' }
            ]]
        });

        date.render({
            elem: '#date',
            range: '~',
            trigger: 'click' //自动弹出控件的事件，采用clik
        });

        form.on('submit(notice-search)', function (data) {
            layui.table.reload('noticeTable', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        //添加弹出层
        $(document).on('click', '.add-notice', function () {
            JsGet("{:url('Notice/add')}", function (e) {
                if (e.status) {
                    window.box = layer.open({
                        type: 1,
                        content: e.data,
                        area: setpage(400, 400),
                        title: '添加公告',
                        btn: ['保存', '关闭'],
                        yes: function () {
                            var formData = form.val("add_notice");
                            JsPost("{:url('Notice/add')}", formData, function (res) {
                                if (res.status) {
                                    layer.msg(res.msg, { time: 1300 }, function () {
                                        layer.closeAll();
                                        table.reload('noticeTable');
                                    });
                                } else {
                                    layer.msg(res.msg);
                                }
                            })
                        }
                    });
                } else {
                    layer.msg(e.msg);
                }
            })
        });

        //监听工具条
        table.on('tool(noticeTable)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'del') { //删除
                layer.confirm('真的要删除吗?', function (index) {
                    JsPost("{:url('Notice/del')}", { id: data.id }, function (res) {
                        if (res.status) {
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);//向服务端发送删除指令
                        }
                        layer.msg(res.msg);
                    });
                });
            } else if (layEvent === 'edit') { //编辑
                JsGet("{:url('Notice/edit')}?id=" + data.id, function (e) {
                    if (e.status) {
                        window.box = layer.open({
                            type: 1,
                            content: e.data,
                            area: setpage(400, 400),
                            title: '修改公告',
                            btn: ['保存', '关闭'],
                            yes: function () {
                                var formData = form.val("edit_notice");
                                JsPost("{:url('Notice/edit')}", formData, function (res) {
                                    if (res.status) {
                                        layer.msg(res.msg, { time: 1300 }, function () {
                                            layer.closeAll();
                                            Object.assign(data,formData);
                                            obj.update(data);
                                        });
                                    } else {
                                        layer.msg(res.msg);
                                    }
                                })
                            }
                        })
                    } else {
                        layer.msg(e.msg);
                    }
                })
            }
        });
    })
</script>
<script type="text/html" id="notice">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>